/*
 * Copyright 2016, Data61, CSIRO (ABN 41 687 119 230)
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#ifndef _RPC_INTERFACE_SERV_CLIENT_HELPER_H_
#define _RPC_INTERFACE_SERV_CLIENT_HELPER_H_

#include <stdarg.h>
#include <refos-rpc/rpc.h>
#include <refos/refos.h>
#include <refos/error.h>
#include <refos-rpc/serv_client.h>
#include <refos-rpc/data_client.h>
#include <refos-rpc/data_client_helper.h>
#include <refos-rpc/name_client.h>
#include <refos-rpc/name_client_helper.h>

/*! @file
    @brief Helper functions for the server connection interface.

    This file contains a simple layer of helper functions that make using the server interface
    much easier, but are too complex to have been generated by the stub generator. 
*/

#ifdef dvprintf
    #define _svprintf dvprintf
#else
    #define _svprintf(...)
#endif

/*! @brief Struct containing the state of an open server connection session.
           This includes the name resolve result, the session, and the set up parameter buffer.
*/
typedef struct serv_connection_s {
    int error;
    nsv_mountpoint_t serverMountPoint; /* Has ownership. */
    seL4_CPtr serverSession;  /* Has ownership. */
    data_mapping_t paramBuffer;  /* Has ownership. */
    bool connectionLess;
} serv_connection_t;

/*! @brief Connect to server at the given path. Helper function for serv_connect_direct(). Set up
           the parameter buffer.
    @param serverPath The namespace path of server to connect to.
    @return Struct containing the open server connection and param buffer info. Check the error
            member of the struct in order to check for failure. (Gives ownership)
*/
serv_connection_t serv_connect(char *serverPath);

/*! @brief Connect to server at the given path. Helper function for serv_connect_direct(). Does not
          set up a parameter buffer.
    @param serverPath The namespace path of server to connect to.
    @return Struct containing the open server connection and param buffer info. Check the error
            member of the struct in order to check for failure. (Gives ownership)
*/
serv_connection_t serv_connect_no_pbuffer(char *serverPath);

/*! @brief Disconnect from the server, unmap and delete parameter buffer, and release the memory
           associated.
    @param sc The server connection state structure to disconnect. Does NOT free the structure
              itself, only frees the resources pointed to by it. (Takes ownership)
*/
void serv_disconnect(serv_connection_t *sc);

#endif /* _RPC_INTERFACE_SERV_CLIENT_HELPER_H_ */